package com.xone.xapp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class SubscribeCreateActivity extends BasicActivity {
	protected final String TAG = SubscribeCreateActivity.class.getSimpleName();
	protected Map<String, String> infoMap = new LinkedHashMap<String, String>();
	protected Spinner spinnerSaleType;
	protected Spinner spinnerCredit;
    protected String saleType;
	protected Map<String, String> saleTypeMap = new LinkedHashMap<String, String>();
	protected Map<String, String> creditMap = new LinkedHashMap<String, String>();
	@Override
	public String getHeaderTitle() {
		return "筛选订阅";
	}
	@Override
	public int getContentView() {
		return R.layout.subscribe_create;
	}
	@Override
	public Context getContext() {
		return this;
	}
	public String getPostURL() {
		return "/subscribe/createJson.html";
	}
	/**
	 * 提交完成后的回调
	 */
	public void doPostFinish(JSONObject result) {
		if (null == result) {
			Toast.makeText(getContext(), "操作失败", Toast.LENGTH_SHORT).show();
			return;
		}
		String msg = "提交成功";
		JSONObject mapValue = JSONUtils.getJSONObjectForKey(result, "mapValue");
		if (mapValue.isNull("msg")) {
			Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
			this.finish();
			return;
		}
		msg = JSONUtils.getStringFromJSONObject(mapValue, "msg");
		Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
	}
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		infoMap.put("marketarea", "");
		infoMap.put("productNameKey", "产品名称不能为空");
		spinnerSaleType = (Spinner)findViewById(R.id.saleType);
		spinnerCredit = (Spinner)findViewById(R.id.credit);
		saleTypeMap.put("全部", "");
		saleTypeMap.put("普通", "0");
		saleTypeMap.put("促销", "1");
		saleTypeMap.put("组团", "2");
		creditMap.put("全部", "");
		creditMap.put("信誉好", "1");
		creditMap.put("信誉一般", "0");
		spinnerSaleType.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Arrays.asList(saleTypeMap.keySet().toArray(new String[0]))));
		spinnerCredit.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Arrays.asList(creditMap.keySet().toArray(new String[0]))));
		Button submitProduct = (Button)findViewById(R.id.submit_subscribe);
		submitProduct.setOnClickListener(new OnClickListener() {
			@SuppressWarnings("unchecked")
			@Override
			public void onClick(final View v) {
				ContextUtils.confirm(getContext(), "确认提交订阅？", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Map<String, String> value = checkAndGetValue(infoMap);
						if (value.isEmpty()) {
							return;
						}
						showProgress();
						String saleType = spinnerSaleType.getSelectedItem().toString();
						String credit = spinnerCredit.getSelectedItem().toString();
						List<NameValuePair> params = new ArrayList<NameValuePair>();
						for (Map.Entry<String, String> info : value.entrySet()) {
							params.add(new BasicNameValuePair("subscribe." + info.getKey(), info.getValue()));
						}
						params.add(new BasicNameValuePair("subscribe.saleType", saleTypeMap.get(saleType)));
						params.add(new BasicNameValuePair("subscribe.credit", creditMap.get(credit)));
						new PostDataTask(getContext(), v).execute(params);
					}
				});
			}
		});
	}
	
	protected boolean notPass(String value, String notice) {
		if (StringUtils.isBlank(value)) {
			Toast.makeText(getContext(), notice, Toast.LENGTH_SHORT).show();
			return true;
		}
		return false;
	}
	
	public Map<String, String> checkAndGetValue(Map<String, String> infoMap) {
		Map<String, String> valueMap = new HashMap<String, String>();
		for (Map.Entry<String, String> info : infoMap.entrySet()) {
			EditText editText = (EditText)findViewById(ContextUtils.getResourceId(getContext(), info.getKey(), "id"));
			String value = editText.getText().toString();
			if (StringUtils.isNotBlank(info.getValue()) && notPass(value, info.getValue())) {
				return Collections.emptyMap();
			}
			valueMap.put(info.getKey(), value);
		}
		return valueMap;
	}
	
	public class PostDataTask extends AbstractPostDataTask {
		protected Context context;
		protected View view;
		public PostDataTask(Context context, View view) {
			super(context);
			this.view = view;
		}
		@Override
		public String getPostUrl() {
			return getPostURL();
		}
		@Override
		public void postFinish(JSONObject result) {
			this.view.setEnabled(true);
			if (null == result) {
				hideProgress();
				return;
			}
			doPostFinish(result);
		}
	}

}
